package com.zige.zige.bj_vr;

import android.app.AlertDialog;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.res.Configuration;
import android.media.Image;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.Uri;
import android.opengl.Visibility;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.SeekBar;
import android.widget.TextView;
import android.widget.Toast;

import com.umeng.socialize.ShareAction;
import com.umeng.socialize.UMShareAPI;
import com.umeng.socialize.UMShareListener;
import com.umeng.socialize.bean.SHARE_MEDIA;
import com.umeng.socialize.media.UMImage;
import com.umeng.socialize.media.UMVideo;
import com.umeng.socialize.media.UMusic;
import com.zige.zige.R;
import com.zige.zige.activity.BaseActivity;
import com.zige.zige.activity.HomedActivity;
import com.zige.zige.constants.Constants;
import com.zige.zige.constants.UrlConsts;
import com.zige.zige.dialog.PromptDialog;
import com.zige.zige.dialog.ZiGeShareDialog;
import com.zige.zige.pojo.UserInfo;
import com.zige.zige.pojo.VideoInfo;
import com.zige.zige.task.PostRequestTask;
import com.zige.zige.task.TaskExecutor;
import com.zige.zige.utils.PreferencesUtils;
import com.zige.zige.utils.SharedPreferencesUtils;
import com.zige.zige.utils.StringUtils;
import com.zige.zige.utils.SystemUtils;
import com.zige.zige.utils.ToastUtils;
import com.zige.zige.view.panoplayer.PanoPlayerView;
import com.zige.zige.view.panoplayer.TitleAndBottomListener;
import com.zige.zige.view.panoplayer.VideoStateListener;

import org.json.JSONObject;

import java.util.Formatter;
import java.util.HashMap;
import java.util.Locale;


/**
 * Created by admin on 16/1/5.
 */
public class BJVrPlayActivity extends BaseActivity implements View.OnClickListener {

    private int operate;
    private String vid;


    private int isPraises;
    private String jxVideoId;
    private String mVideoUrl;
    private String videoTitle, videoContext, videoImageUrl;

    private ImageButton mBtnPlay;
    private ImageButton mBtnPause;
    private Button mBtnControlMode;
    private Button mBtnDisplayMode;
    private Button mBtnFullscreen;
    private SeekBar mSeekBar;
    private PanoPlayerView mPanoView;

    private TextView tv_title;
    private LinearLayout vrplay_back;
    private ImageView shareBtn, zanBtn, center_time_btn;
    private RelativeLayout rlt_vrplay_zan, rlt_vrplay_share;
    private UserInfo userInfo;

    private LinearLayout center_paly_lyt;
    private TextView mEndTime, mCurTime, center_time1, center_time2;

    private boolean isControlMode = true;
    private boolean isDisplayMode = true;

    private Handler mHandler = new Handler() {
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case 1:
                    if (canHideTitle) {
                        lay_title.setVisibility(View.GONE);
                        lay_foot.setVisibility(View.GONE);
                        isShow = false;
                    }

                    mHandler.postDelayed(mThread, 5000);
                    break;

                case 2:
                    if (isShow) {
                        setTime();
                    }
                    mHandler.postDelayed(setTimeThread, 1000);
                    break;
                default:
                    break;
            }
        }
    };

    private Thread mThread = new Thread(new Runnable() {
        @Override
        public void run() {
            Message message = new Message();
            message.what = 1;
            mHandler.sendMessage(message);
        }
    });

    private Thread setTimeThread = new Thread(new Runnable() {
        @Override
        public void run() {
            Message message = new Message();
            message.what = 2;
            mHandler.sendMessage(message);
        }
    });


    private LinearLayout lay_foot;
    private RelativeLayout lay_title;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        if (savedInstanceState != null) {
            savedInstanceState = null;
            try {
                startActivity(new Intent(getApplicationContext(), BJVrPlayActivity.class));
                finish();
            } catch (Exception e) {
                e.printStackTrace();
            }
            return;
        }
        super.onCreate(savedInstanceState);

        this.requestWindowFeature(Window.FEATURE_NO_TITLE);//去掉标题栏
        setContentView(R.layout.activity_bj_vrplay);
        userInfo = application.getUserInfo();
        initView();
        alert = new AlertDialog.Builder(this);
        MyAsync myAsync = new MyAsync();
        myAsync.execute(getIntent());
    }

    private void initView() {
        mCurTime = (TextView) findViewById(R.id.current_time);
        mEndTime = (TextView) findViewById(R.id.time_all);
        mStrBuilder = new StringBuilder();
        mFormatter = new Formatter(mStrBuilder, Locale.getDefault());

        center_paly_lyt = (LinearLayout) findViewById(R.id.center_paly_lyt);
        center_time1 = (TextView) findViewById(R.id.center_time1);
        center_time2 = (TextView) findViewById(R.id.center_time2);
        center_time_btn = (ImageView)findViewById(R.id.center_time_btn);

        lay_title = serchViewById(R.id.lay_title);
        lay_foot = serchViewById(R.id.lay_foot);
        mPanoView = (PanoPlayerView) findViewById(R.id.pano_view);
        mBtnPlay = (ImageButton) findViewById(R.id.btn_play);
        mBtnPause = (ImageButton) findViewById(R.id.btn_pause);
        mBtnControlMode = (Button) findViewById(R.id.btn_control_mode);
        mPanoView.setGyroControlEnabled(isControlMode);
        mPanoView.setTitleAndBottomListener(new TitleAndBottomListener() {
            @Override
            public void showOrhide() {
                if (!isDisplayMode) {
                    if (mPanoView.getVideoPlayer().isPlaying()) {
                        mPanoView.getVideoPlayer().pause();
                        setCenterTime();
                    } else {
                        mPanoView.getVideoPlayer().play();
                        center_paly_lyt.setVisibility(View.GONE);
                        center_time_btn.setVisibility(View.GONE);
                    }
                } else {
                    if (isShow) {
                        lay_title.setVisibility(View.GONE);
                        lay_foot.setVisibility(View.GONE);
                        isShow = false;
                    } else {
                        lay_title.setVisibility(View.VISIBLE);
                        lay_foot.setVisibility(View.VISIBLE);
                        isShow = true;
                    }
                }
            }
        });
        mBtnControlMode.setBackgroundResource(R.drawable.tuoluoyi_select);
        isControlMode=false;
        mBtnDisplayMode = (Button) findViewById(R.id.btn_display_mode);
        mBtnFullscreen = (Button) findViewById(R.id.btn_fullscreen);
        mSeekBar = (SeekBar) findViewById(R.id.seek_bar);

        tv_title = serchViewById(R.id.back_text);
        vrplay_back = serchViewById(R.id.vrplay_back);
        vrplay_back.setOnClickListener(this);

        zanBtn = serchViewById(R.id.btn_vrplay_zan);
//        zanBtn.setOnClickListener(this);
        shareBtn = serchViewById(R.id.btn_vrplay_share);
//        shareBtn.setOnClickListener(this);

        rlt_vrplay_share = serchViewById(R.id.rlt_vrplay_share);
        rlt_vrplay_share.setOnClickListener(this);
        rlt_vrplay_zan = serchViewById(R.id.rlt_vrplay_zan);
        rlt_vrplay_zan.setOnClickListener(this);

        mBtnPlay.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (null == mPanoView) {
                    return;
                }
                if (mPanoView.getVideoPlayer().isPlaying()) {
                    mPanoView.getVideoPlayer().pause();
                    mBtnPlay.setVisibility(View.VISIBLE);
                    mBtnPause.setVisibility(View.GONE);
                } else {
                    mPanoView.getVideoPlayer().play();
                    mBtnPlay.setVisibility(View.GONE);
                    mBtnPause.setVisibility(View.VISIBLE);
                }

            }
        });
        mBtnPause.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (null == mPanoView) return;
                mPanoView.getVideoPlayer().pause();
                mBtnPause.setVisibility(View.GONE);
                mBtnPlay.setVisibility(View.VISIBLE);
            }
        });
        mBtnControlMode.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (isControlMode) {
                    mPanoView.setGyroControlEnabled(isControlMode);
                    mBtnControlMode.setBackgroundResource(R.drawable.tuoluoyi_select);
                } else {
                    mBtnControlMode.setBackgroundResource(R.drawable.tuoluoyi);
                    mPanoView.setGyroControlEnabled(isControlMode);
                }
                isControlMode = !isControlMode;
            }
        });
        mBtnDisplayMode.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                if (isDisplayMode) {
                    lay_title.setVisibility(View.GONE);
                    lay_foot.setVisibility(View.GONE);
                    isShow = false;

                    mBtnDisplayMode.setBackgroundResource(R.drawable.btn_vr_on);
                    mPanoView.setVrModeEnabled(isDisplayMode);
                } else {
                    mBtnDisplayMode.setBackgroundResource(R.drawable.btn_vr_off);
                    mPanoView.setVrModeEnabled(isDisplayMode);
                }
                isDisplayMode = !isDisplayMode;

                if (!mPanoView.getVideoPlayer().isPlaying() && !isDisplayMode) {
                    setCenterTime();
                } else {
                    center_paly_lyt.setVisibility(View.GONE);
                    center_time_btn.setVisibility(View.GONE);
                }
            }
        });

        center_time_btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mBtnDisplayMode.setBackgroundResource(R.drawable.btn_vr_off);
                mPanoView.setVrModeEnabled(isDisplayMode);
                isDisplayMode = !isDisplayMode;

                center_paly_lyt.setVisibility(View.GONE);
                center_time_btn.setVisibility(View.GONE);

                lay_title.setVisibility(View.VISIBLE);
                lay_foot.setVisibility(View.VISIBLE);
                isShow = true;
            }
        });

        mBtnFullscreen.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

            }
        });
        mSeekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {

                if (fromUser) {
                    mPanoView.getVideoPlayer().seekToPercentage(progress / 100.0);
                    double ii = mPanoView.getVideoPlayer().getDuration();
                    mPanoView.getVideoPlayer().getPreloadTime();

                }
//                long duration = (long) mPanoView.getVideoPlayer().getDuration();
//                long newposition = (duration * progress) / 1000L;
//                if (mCurTime != null)
//                    mCurTime.setText(stringForTime((int) newposition));
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {
                mPanoView.getVideoPlayer().beforeSeek();
                canHideTitle = false;
            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {
                mPanoView.getVideoPlayer().seekToPercentage(seekBar.getProgress() / 100.0);
                mPanoView.getVideoPlayer().afterSeek();
                canHideTitle = true;

                setTime();
            }
        });

        mHandler.postDelayed(mThread, 4000);
    }

    StringBuilder mStrBuilder;
    Formatter mFormatter;

    private String stringForTime(int timeMs) {
        int totalSeconds = timeMs / 1000;

        int seconds = totalSeconds % 60;
        int minutes = (totalSeconds / 60) % 60;
        int hours = totalSeconds / 3600;

        mStrBuilder.setLength(0);
        if (hours > 0) {
            return mFormatter.format("%d:%02d:%02d", hours, minutes, seconds)
                    .toString();
        } else {
            return mFormatter.format("%02d:%02d", minutes, seconds).toString();
        }
    }

    private boolean canHideTitle = true;

    private void play(String videoUrl) {
        if (application.isLogin()) {
            userWatchRecord(vid);
        }
        if (StringUtils.isEmpty(videoUrl)) {
            showErrorDialog("视频链接不可用");
            return;
        }
        mPanoView.getVideoPlayer().prepare(videoUrl);
        mPanoView.getVideoPlayer().setVideoStateListener(new myVideoStateListener());
        if( mPanoView.getVideoPlayer().isPlaying()){
            closeLoadingDialog();
        }
    }

    boolean isShow = true;

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.vrplay_back:
                finish();
                break;
            case R.id.rlt_vrplay_zan:
                if (application.isLogin()) {
                    setPressPraise(operate);
                } else {
                    PromptDialog dialog = new PromptDialog();
                    dialog.show(getFragmentManager(), "dialo");
                }
                break;
            case R.id.rlt_vrplay_share:
                mPanoView.getVideoPlayer().pause();
                sharesina();
                break;
        }
    }

    class myVideoStateListener implements VideoStateListener {
        @Override
        public void onPrepared(boolean succeed) {
            if (succeed) {
                mHandler.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        mBtnPlay.setEnabled(true);
                        mPanoView.getVideoPlayer().play();
                    }
                }, 1500);

                mHandler.postDelayed(setTimeThread, 1000);
            }
        }

        @Override
        public void onStarted() {
            mBtnPlay.setVisibility(View.GONE);
            mBtnPause.setVisibility(View.VISIBLE);
        }

        @Override
        public void onPaused(boolean needBuffer) {
            mBtnPause.setVisibility(View.GONE);
            mBtnPlay.setVisibility(View.VISIBLE);
        }

        @Override
        public void onResumed() {
            mBtnPlay.setVisibility(View.GONE);
            mBtnPause.setVisibility(View.VISIBLE);
        }

        @Override
        public void onProgress(float currentPercentge, float loadedPercentage) {
            mSeekBar.setProgress((int) (currentPercentge * 100));
            mSeekBar.setSecondaryProgress((int) (loadedPercentage * 100));

//            setTime();
        }

        @Override
        public void onStopped() {
            mBtnPause.setVisibility(View.GONE);
            mBtnPlay.setVisibility(View.VISIBLE);
        }
    }

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);

    }

    int position;
    int duration;

    private void setTime() {
        if (mPanoView.getVideoPlayer() == null) return;

        try {
            position = mPanoView.getVideoPlayer().getCurPosition();
            duration = (int) mPanoView.getVideoPlayer().getDuration();
        } catch (Exception e){
            e.printStackTrace();
        }

        if (mEndTime != null)
            mEndTime.setText(stringForTime(duration));

        if (mCurTime != null)
            mCurTime.setText(stringForTime(position));
    }

    private void setCenterTime() {
        if (mPanoView.getVideoPlayer() == null) return;

        try {
            position = mPanoView.getVideoPlayer().getCurPosition();
            duration = (int) mPanoView.getVideoPlayer().getDuration();
        } catch (Exception e){
            e.printStackTrace();
        }

        center_paly_lyt.setVisibility(View.VISIBLE);
        center_time_btn.setVisibility(View.VISIBLE);

        center_time2.setText(stringForTime(position) + " | " + stringForTime(duration));
        center_time1.setText(stringForTime(position) + " | " + stringForTime(duration));
    }

    private void videoPlay() {
        mPanoView.getVideoPlayer().play();
        mBtnPlay.setVisibility(View.GONE);
        mBtnPause.setVisibility(View.VISIBLE);
    }

    private void pausePlay() {
        mPanoView.getVideoPlayer().pause();
        mBtnPause.setVisibility(View.GONE);
        mBtnPlay.setVisibility(View.VISIBLE);
    }

    String shareImageUrl;
    private void getVideoDetils(String videoId) {
        String uid;
        if(application.isLogin()){
            uid = userInfo.userID;
        }else {
            uid="";
        }
        HashMap<String, String> params = new HashMap<>();
        params.put("vid", videoId);
        params.put("userId", uid);
        showLoadingDialog();
        TaskExecutor.getInstance().execute(new PostRequestTask(params, BJVrPlayActivity.this, UrlConsts.VIDEO_DETILS) {
            @Override
            protected void handleResponse(String response) {
                Log.e("wu","rrrr   "+response);
                try {
                    JSONObject object = new JSONObject(response);
                    int ret = object.getInt("code");
                    String msg = object.getString("message");
                    if (ret == 0000) {
                        object = object.getJSONObject("jxVideo");
                        jxVideoId = object.getString("id");
                        videoTitle = object.getString("videoName");
                        tv_title.setText(videoTitle);
                        videoContext = object.getString("videoDesc");
                        mVideoUrl = object.getString("videoUrl");
                        isPraises = object.getInt("praised");
                        videoImageUrl = object.getString("videoPicUrl");
                        shareImageUrl = object.getString("sharePicUrl");
                        play(mVideoUrl);
                        if (application.isLogin()) {
                            if (isPraises == 1) {
                                zanBtn.setImageResource(R.drawable.videoplay_press_zan);
                                operate = 1;
                            } else {
                                zanBtn.setImageResource(R.drawable.videoplay_zan);
                                operate = 0;
                            }
                        } else {
                            zanBtn.setImageResource(R.drawable.videoplay_zan);

                        }

                    } else {
                        throw new Exception(msg);
                    }
                } catch (Exception e) {
                    closeLoadingDialog();
                } finally {
                    closeLoadingDialog();
                }
            }

            @Override
            protected void handleError() {
                super.handleError();
            }
        });
    }

    /***
     * 点赞
     *
     * @param operated
     */
    private void setPressPraise(final int operated) {
        String deviceId = SystemUtils.getDeviceKey(BJVrPlayActivity.this);
        HashMap<String, String> params = new HashMap<>();
        params.put("deviceId", deviceId);
        params.put("uid", userInfo.userID);
        Log.e("wu", userInfo.userID + "    " + String.valueOf(operated));
        params.put("account", userInfo.phoneNumber);
        params.put("jxVideoId", jxVideoId);
        params.put("operate", String.valueOf(operated));
        TaskExecutor.getInstance().execute(new PostRequestTask(params, BJVrPlayActivity.this, UrlConsts.VIDEO_PRAISE) {
            @Override
            protected void handleResponse(String response) {
                try {
                    JSONObject object = new JSONObject(response);
                    int ret = object.getInt("code");
                    String msg = object.getString("message");
                    if (ret == 0000) {
                        if (operated == 0) {
                            zanBtn.setImageResource(R.drawable.videoplay_press_zan);
                            operate = 1;
                        }
                        if (operated == 1) {
                            zanBtn.setImageResource(R.drawable.videoplay_zan);
                            operate = 0;
                        }
                    } else {
                        throw new Exception(msg);
                    }
                } catch (Exception e) {
                    ToastUtils.showToast(BJVrPlayActivity.this, e.getMessage());
                } finally {

                }
            }
        });
    }

    class MyAsync extends AsyncTask<Intent, Integer, String> {
        @Override
        protected String doInBackground(Intent... params) {
            Intent intent = params[0];
            vid = intent.getStringExtra("videoId");
            registerReceiver();
            publishProgress(100);
            return null;
        }

        @Override
        protected void onProgressUpdate(Integer... values) {
            if (100 == values[0])
                if (vid != null) {
                    getVideoDetils(vid);
                } else {
                    getVideoDetils(vid);
                }

            super.onProgressUpdate(values);
        }

    }

    //向服务器发送已登录用户观看记录
    private void userWatchRecord(String videoId) {
        HashMap<String, String> params = new HashMap<>();
        params.put("uid", application.getUserInfo().userID);
        params.put("videoId", videoId);
        TaskExecutor.getInstance().execute(new PostRequestTask(params, BJVrPlayActivity.this, UrlConsts.USER_WATCH_RECORD) {
            @Override
            protected void handleResponse(String response) {

            }
        });
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        mPanoView.getVideoPlayer().cleanUp();
        unregisterReceiver();
    }

    NetworkStateChangedReceiver myReceiver;

    private void registerReceiver() {
        boolean isWifiOnly = PreferencesUtils.getBoolean(this, Constants.PREFERENCE_KEY_IS_WIFI_ONLY);
        boolean on = (boolean) SharedPreferencesUtils.getValue(this, SharedPreferencesUtils.gesture_toggle, false);
        if (on || isWifiOnly) {
            return;
        }
        IntentFilter filter = new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION);
        myReceiver = new NetworkStateChangedReceiver();
        this.registerReceiver(myReceiver, filter);
    }

    private void unregisterReceiver() {
        if (myReceiver != null) {
            this.unregisterReceiver(myReceiver);
            myReceiver = null;
        }
    }

    AlertDialog.Builder alert;

    class NetworkStateChangedReceiver extends BroadcastReceiver {

        @Override
        public void onReceive(Context context, Intent intent) {
            ConnectivityManager connectivityManager = (ConnectivityManager) context
                    .getSystemService(Context.CONNECTIVITY_SERVICE);
            NetworkInfo mobNetInfo = connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
            NetworkInfo wifiNetInfo = connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);

            if (!mobNetInfo.isConnected() && !wifiNetInfo.isConnected()) {
                ToastUtils.showToast(context, "网络不可以用");
                // 改变背景或者 处理网络的全局变量
            } else if (!wifiNetInfo.isConnected()) {
                pausePlay();
                AlertDialog.Builder alert = new AlertDialog.Builder(context);
                alert.setCancelable(false);
                alert.setTitle("提示");
                alert.setMessage("您正在使用非WIFI网络，播放将产生流量费用");
                alert.setPositiveButton("继续播放", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
//                        videoPlay();
                    }
                }).setNegativeButton("返回", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
//                        pausePlay();
                        finish();
                    }
                }).show();
            }

        }

    }


    private UMImage shareVideoImageUrl,sinaImageUrl;
    private String shareVideoUrl;
    private void sharesina() {
        ZiGeShareDialog dialog = new ZiGeShareDialog();
        dialog.setSharelister(zigeshareLister);
        dialog.show(getFragmentManager(), "dialog");

        if (TextUtils.isEmpty(videoTitle)) {
            videoTitle = "VR小伴马";
        }

        shareVideoUrl = UrlConsts.SHARE_VIDEIO_URL + jxVideoId;
        Log.e("wu","分享图片   "+shareImageUrl);
        shareVideoImageUrl = new UMImage(BJVrPlayActivity.this, videoImageUrl);
        sinaImageUrl = new UMImage(BJVrPlayActivity.this, shareImageUrl);
    }

    private UMShareListener umShareListener = new UMShareListener() {
        @Override
        public void onResult(SHARE_MEDIA platform) {
            com.umeng.socialize.utils.Log.d("plat", "platform" + platform);
            Toast.makeText(BJVrPlayActivity.this, " 分享成功啦", Toast.LENGTH_SHORT).show();
        }

        @Override
        public void onError(SHARE_MEDIA platform, Throwable t) {
            Toast.makeText(BJVrPlayActivity.this," 分享失败啦", Toast.LENGTH_SHORT).show();
        }

        @Override
        public void onCancel(SHARE_MEDIA platform) {
            Toast.makeText(BJVrPlayActivity.this," 分享取消了", Toast.LENGTH_SHORT).show();
        }
    };


    private ZiGeShareDialog.ZigeshareLister zigeshareLister = new ZiGeShareDialog.ZigeshareLister() {
        @Override
        public void shareSina() {
//            UMImage image = new UMImage(BJVrPlayActivity.this, "http://www.umeng.com/images/pic/social/integrated_3.png");
            new ShareAction(BJVrPlayActivity.this).setPlatform(SHARE_MEDIA.SINA).setCallback(umShareListener)
                    .withText("分享给大家一个好看的视频：")
                    .withMedia(sinaImageUrl)
//                       .withExtra(new UMImage(ShareActivity.this,R.drawable.ic_launcher))
                    .withTargetUrl(shareVideoUrl)
                    .share();
        }

        @Override
        public void shareWeiXin() {
            new ShareAction(BJVrPlayActivity.this).setPlatform(SHARE_MEDIA.WEIXIN).setCallback(umShareListener)
                    .withTitle(videoTitle)
                    .withText("分享给大家一个好看的视频：")
                    .withMedia(shareVideoImageUrl)
                    .withTargetUrl(shareVideoUrl)
                    .share();
        }

        @Override
        public void shareWeiXinCircle() {
            new ShareAction(BJVrPlayActivity.this).setPlatform(SHARE_MEDIA.WEIXIN_CIRCLE).setCallback(umShareListener)
                    .withTitle(videoTitle)
                    .withText("分享给大家一个好看的视频：")
                    .withMedia(shareVideoImageUrl)
                    .withTargetUrl(shareVideoUrl)
                    .share();
        }
    };

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        /** attention to this below ,must add this**/
        UMShareAPI.get(this).onActivityResult(requestCode, resultCode, data);
    }

}
